package net.courier.remoting.provider.netty;

import io.netty.channel.Channel;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Promise;
import net.courier.remoting.protocol.ResponseDatagram;

public class ClientPromise extends DefaultPromise<ResponseDatagram> implements Promise<ResponseDatagram> {

	private final Channel channel;

	/**
	 * Creates a new instance.
	 * 
	 * @param channel
	 *            the {@link Channel} associated with this future
	 */
	public ClientPromise(Channel channel) {
		this.channel = channel;
	}

	/**
	 * Creates a new instance.
	 * 
	 * @param channel
	 *            the {@link Channel} associated with this future
	 */
	public ClientPromise(Channel channel, EventExecutor executor) {
		super(executor);
		this.channel = channel;
	}

	@Override
	protected EventExecutor executor() {
		EventExecutor e = super.executor();
		if (e == null) {
			return channel.eventLoop();
		} else {
			return e;
		}
	}

	@Override
	public ClientPromise setSuccess(ResponseDatagram result) {
		super.setSuccess(result);
		return this;
	}
}